import os, sys, getopt, subprocess, shutil

options, remainder = getopt.getopt(sys.argv[1:], 'I:O:')
include_dir = "[path to source directory]"
out_dir = "[path to moc file output directory - best to use source directory here also]"
pub_dir = "[path to public directory]"

#only supports headers that define a single class
def walker(arg, dirname, names):
	for name in names:
		fname = dirname + '\\' + name

		if fname.endswith('.h'):
			#copy all header files to the public folder
			full_file_name = os.path.join(include_dir, fname)
			shutil.copy(full_file_name, pub_dir)
			f = open(fname, 'r')
			try:
				lines = f.read()
			finally:
				f.close()

			if lines.find('Q_OBJECT') >= 0:
				#run moc compiler
				i = "-I"+dirname

				if dirname == include_dir:
					o = "-o"+out_dir+"\moc_"+name[:-2]+".cpp"
				else:
					l = len(include_dir)
					subdirs = dirname[l:]
					o = "-o"+out_dir+subdirs+"\moc_"+name[:-2]+".cpp"
				#usually located in C:\bbndk\host_[targetSDK version]\win32\x86\usr\bin
				subprocess.call(["[path to moc.exe here]", i, o, fname])
				
for opt, arg in options:
	if opt == '-I':
		include_dir = arg
	elif opt == '-O':
		out_dir = arg

if len(include_dir) != 0 and len(out_dir) != 0:
	os.path.walk(include_dir, walker, None)